package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.WorksheetCollectDAO;
import com.proactive.webapp.entity.Worksheet;
import com.proactive.webapp.entity.WorksheetCollect;
import com.proactive.webapp.util.DateConversion;
import com.proactive.webapp.util.EncodeHtml;

public class WorksheetCollectDAOImpl implements WorksheetCollectDAO {
	DateConversion dateConversion = new DateConversion();
	EncodeHtml encodeHtml = new EncodeHtml();
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public void insert(WorksheetCollect worksheetCollect) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO worksheet_collect(worksheetID, memberAccount, createDate) VALUES(?,?,NOW())";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetCollect.getWid());
			smt.setString(2, worksheetCollect.getAccount());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public void delete(WorksheetCollect worksheetCollect) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM worksheet_collect WHERE worksheetID=? AND memberAccount=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetCollect.getWid());
			smt.setString(2, worksheetCollect.getAccount());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public int countTotal(String account) {
		// TODO Auto-generated method stub
		String sql = "SELECT *,(SELECT COUNT(*) FROM worksheet_report d WHERE a.worksheetID = d.worksheetID HAVING COUNT(*)<10) "
				+ "AS reportCount FROM worksheet_collect a JOIN worksheet e ON a.worksheetID = e.worksheetID "
				+ "WHERE a.memberAccount = ? AND worksheetStatus = 1 HAVING reportCount IS NOT NULL";
		int count = 0;
		try {			
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, account);
			rs = smt.executeQuery();
			rs.beforeFirst();  
			rs.last(); 
			count =rs.getRow();
			
			rs.close();
			smt.close();
			
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return count;
	}

	@Override
	public List<Worksheet> getList(String loginAccount, String account, int start, int size) {
		// TODO Auto-generated method stub
		List<Worksheet> worksheetList = new ArrayList<Worksheet>();
		String sql = "SELECT *,(SELECT COUNT(*) FROM worksheet_comment b WHERE topCommentID IS NULL AND a.worksheetID = b.worksheetID) "
				+ "AS commentCount,(SELECT COUNT(*) FROM worksheet_favorite c WHERE a.worksheetID = c.worksheetID) "
				+ "AS favoriteCount,(SELECT COUNT(*) FROM worksheet_report d WHERE a.worksheetID = d.worksheetID HAVING COUNT(*)<10) "
				+ "AS reportCount,(SELECT COUNT(*) FROM worksheet_collect e WHERE a.worksheetID = e.worksheetID AND e.memberAccount = ?) "
				+ "AS collect FROM worksheet_collect a JOIN worksheet f ON a.worksheetID = f.worksheetID "
				+ "WHERE a.memberAccount = ? AND worksheetStatus = 1 HAVING reportCount IS NOT NULL "
				+ "ORDER BY a.createDate DESC LIMIT ?,?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, loginAccount);
			smt.setString(2, account);
			smt.setInt(3, start);
			smt.setInt(4, size);
			rs = smt.executeQuery();
			while(rs.next()){
				Worksheet worksheet = new Worksheet();
				worksheet.setId(rs.getInt("worksheetID"));
				worksheet.setTitle(rs.getString("worksheetTitle"));
				worksheet.setStatus(rs.getInt("worksheetStatus"));
				worksheet.setContent(rs.getString("worksheetContent"));
				worksheet.setGist(encodeHtml.getGist(96, worksheet.getContent()));
				worksheet.setCreateDate(rs.getDate("createDate"));
				worksheet.setCreateDateString(dateConversion.ConversionYDM(worksheet.getCreateDate()));
				worksheet.setViewCount(rs.getInt("worksheetViewCount"));
				worksheet.setFavoriteCount(rs.getInt("favoriteCount"));
				worksheet.setCommentCount(rs.getInt("commentCount"));
				worksheet.setCollectFlag(rs.getInt("collect")==0?false:true);
				worksheet.setCollectString(worksheet.isCollectFlag()?"已收藏":"收藏");
				worksheet.setCollectString1(worksheet.isCollectFlag()?"none":"inline");
				worksheet.setCollectString2(worksheet.isCollectFlag()?"inline":"none");
				worksheet.setCollectClass(worksheet.isCollectFlag()?"topfocusCollect":"");
				worksheetList.add(worksheet);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return worksheetList;
	}
}
